package com.yupi.springbootinit.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.DeleteRequest;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.model.dto.unit.UnitAddRequest;
import com.yupi.springbootinit.model.dto.unit.UnitQueryRequest;
import com.yupi.springbootinit.model.dto.unit.UnitUpdateRequest;
import com.yupi.springbootinit.model.entity.Unit;
import com.yupi.springbootinit.service.UnitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * 信息接口
 *
 * @author zlc
 */
@RestController
@RequestMapping("/unit")
@Slf4j
public class UnitController {

    @Resource
    private UnitService unitService;

    /**
     * 创建
     *
     * @param unitAddRequest
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addUnit(@RequestBody UnitAddRequest unitAddRequest) {
        long result = unitService.addUnit(unitAddRequest);
        return ResultUtils.success(result);
    }

    /**
     * 删除
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteUnit(@RequestBody DeleteRequest deleteRequest) {
        boolean result = unitService.deleteUnit(deleteRequest);
        return ResultUtils.success(result);
    }

    /**
     * 更新
     *
     * @param unitUpdateRequest
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateUnit(@RequestBody UnitUpdateRequest unitUpdateRequest) {
        boolean result = unitService.updateUnit(unitUpdateRequest);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<Unit> getUnitById(long id) {
        Unit unit = unitService.getUnitById(id);
        return ResultUtils.success(unit);
    }

    /**
     * 获取列表
     *
     * @param unitQueryRequest
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<Unit>> listUnit(UnitQueryRequest unitQueryRequest) {
        List<Unit> list = unitService.listUnit(unitQueryRequest);
        return ResultUtils.success(list);
    }

    /**
     * 分页获取列表
     *
     * @param unitQueryRequest
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<Unit>> listUnitByPage(UnitQueryRequest unitQueryRequest) {
        Page<Unit> page = unitService.listUnitByPage(unitQueryRequest);
        return ResultUtils.success(page);
    }
}
